package cl.blueprintsit.filmdisk.utils;

import cl.blueprintsit.filmdisk.model.Actor;
import cl.blueprintsit.filmdisk.model.Film;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import static java.util.Collections.emptyList;

/**
 * Created with IntelliJ IDEA.
 * User: Andres
 * Date: 21-10-13
 * Time: 07:01 AM
 * To change this template use File | Settings | File Templates.
 */
public class DirectoryFilmParser {

    public Film parseFilm(File directoryFilm) {

        Film theParsedFilm = new Film(directoryFilm);
        List<Actor> actors = parseActors(directoryFilm);
        theParsedFilm.addActors(actors);

        return theParsedFilm;
    }

    private static List<Actor> parseActors(File directoryFilm) {

        List<Actor> actors = new ArrayList<Actor>();

        String filmAbsolutePath = directoryFilm.getAbsolutePath();
        int indexActorBeginSection = filmAbsolutePath.indexOf('(');
        if(indexActorBeginSection < 0){
            return emptyList();
        }

        int indexActorEndSection = filmAbsolutePath.indexOf(')', indexActorBeginSection);
        if (indexActorEndSection < 0){
            return emptyList();
        }

        String actorsString = filmAbsolutePath.substring(indexActorBeginSection+1, indexActorEndSection);

        StringTokenizer actorsStringBuffer = new StringTokenizer(actorsString, "&,");
        while (actorsStringBuffer.hasMoreElements()) {
            String actorName = actorsStringBuffer.nextToken();
            actors.add(new Actor(actorName));
        }

        return actors;
    }
}
